-- lighthouse
-- Created by panyl Jan/13/2017
-- 灯塔

function initLighthouse(self)
    local class = self.gridData.class;
    local element = self.gridData.element;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);
    local args = DungeonM.queryElement(element, "args");

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    -- local statue = self.gridData.statue;
    -- local name = ArtStatueM.query(statue, "name");

    -- local pos = self.gridData:getPos();

    redrawLighthouse(self);

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 如果已经完成了
            local statField = args["bonus_stat"] or "";
            local clickTimes = args["click_times"] or 1;
            if BuildingBonusM.getFieldStat(statField) > 0 or CombatStatM.getFieldStat(statField) >= clickTimes then
                -- 无效了
                return;
            end

            -- 点击一下
            DungeonActionM.go("touch_lighthouse", self.gridData:getPos());

            -- playEffect(self, 995, 25, -20, nil, 0.9);
        end
    end
    self.icon:addTouchEventListener(onClicked);
end

function redrawLighthouse(self)
    local class = self.gridData.class;
    local element = self.gridData.element;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);
    local args = DungeonM.queryElement(element, "args");

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    -- 如果已经完成了
    local statField = args["bonus_stat"] or "";
    local clickTimes = args["click_times"] or 1;
    if BuildingBonusM.getFieldStat(statField) > 0 or CombatStatM.getFieldStat(statField) >= clickTimes then
        iconPath = getLevelItemIconPath(buildingInfo["icon2"]);
    end

    self:cleanItem();

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:setVisible(true);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
    self.hasShadow = false;

    self:initGridState();
end
